<?php
class Help_issue extends CS_Controller
{
    public function _init() 
    {
        $this->load->library('pagination');
        $this->load->model('help_issue_model','help_issue');
    }
    
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $getData['provider_id'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('help_issue/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('help_issue/grid');
        $config['total_rows'] = $this->help_issue->findByCount($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['list'] = $this->help_issue->findPageList($num, $page_num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('help_issue/grid', $data);
    }
    
    public function add()
    {
        $this->load->view('help_issue/add');
    }
    
    public function addPost()
    {
        $this->validate();
        $getData = $this->input->post();
        $getData['provider_id'] = $this->uid;
        $getData['created_at'] = date('Y-m-d H:i:s');
        $insert_id = $this->help_issue->insert($getData);
        if (!$insert_id){
            $this->ajaxJson(false,'添加失败');
        }
        $this->ajaxJson(true,site_url('help_issue/grid'));
    }
    
    public function edit($id)
    {
        $res = $this->help_issue->findOne(['id' => $id]);
        $data['question'] = $res->row();
        $this->load->view('help_issue/edit', $data);
    }
    
    public function editPost()
    {
        $this->validate();
        $res = $this->help_issue->findOne(['id' => $this->input->post('id'), 'provider_id' => $this->uid]);
        if ($res->num_rows() <= 0){
            $this->ajaxJson(false,'该问题不存在');
        }
        $getData = $this->input->post();
        $id = $this->input->post('id');
        unset($getData['id']);
        $getData['updated_at'] = date('Y-m-d H:i:s');
        $state = $this->help_issue->update($getData,['id' => $id]);
        if (!$state){
            $this->ajaxJson(false,'添加失败');
        }
        $this->ajaxJson(true,site_url('help_issue/grid'));
    }
    
    public function  delete($id)
    {
        $res = $this->help_issue->findOne(['id' => $id, 'provider_id' => $this->uid]);
        if ($res->num_rows() <= 0){
            $this->ajaxJson(false,'该问题不存在');
        }
        $state = $this->help_issue->delete($id);
        if (!$state){
            $this->error('help_issue/grid', '', '删除失败');
        }
        $this->success('help_issue/grid', '', '删除成功');
    }
    
    public function validate()
    {
        if ($this->validateParam($this->input->post('issue_title'))){
            $this->ajaxJson(false,'标题不能为空');
        }
        if ($this->validateParam($this->input->post('issue_answer'))){
            $this->ajaxJson(false,'答案不能为空');
        }
        if (!in_array($this->input->post('status'), [0,1])){
            $this->ajaxJson(false,'状态参数异常');
        }
        if ($this->validateParam($this->input->post('sort')) || !is_numeric($this->input->post('sort'))){
            $this->ajaxJson(false,'排序不能为空，且必须为数字');
        }
    }
}